<template>
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="80vw" append-to-body @close="cancel">
    <el-form ref="form" :model="form" :rules="editFormParam.editFormRules" label-width="auto" style="padding: 0 24px">
      <form-block-title :title="editFormParam.formTitle"/>
      <el-row :gutter="48">
        <template v-for="(formField) in editFormParam.editFormFieldList">
          <el-col v-if="formField.columnType === 'SELECT'" :sm="12" :xs="24">
            <el-form-item :label="formField.chineseName" :prop="formField.columnName">
              <el-select size="small"  v-model="form[formField.columnName]" placeholder="请选择" style="width: 100%">
                <el-option v-for="item in getDict(formField.dict)" :key="item.dictValue"
                           :label="item.dictLabel"
                           :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="formField.columnType === 'INPUT'" :sm="12" :xs="24">
            <el-form-item
              :label="formField.chineseName" :prop="formField.columnName"
            >
              <el-input size="small"  style="width: 100%" v-model="form[formField.columnName]" :placeholder="'请输入'+formField.chineseName"
              >
                <template slot="append" v-if="formField.unit">{{formField.unit}}</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col v-if="formField.columnType === 'DATE'" :sm="12" :xs="24">
            <el-form-item :label="formField.chineseName" :prop="formField.columnName"
            >
              <el-date-picker size="small" v-model="form[formField.columnName]" :picker-options="pickerOptions"
                              placeholder="请选择时间" style="width: 100%" type="date"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col v-if="formField.columnType === 'DATETIME'" :sm="12" :xs="24">
            <el-form-item>
              <el-date-picker  v-model="form[formField.columnName]" placeholder="请选择日期"
                              size="small"
                              style="width:100%" type="month" value-format="yyyy-MM" :picker-options="daterangeOptions"
              />
            </el-form-item>
          </el-col>
          <el-col v-if="formField.columnType === 'DATERANGE'" :sm="12" :xs="24">
            <el-form-item>
              <el-date-picker
                v-model="form[formField.columnName]"
                type="daterange"
                size="small"
                style="width: 100%"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <slot></slot>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">关 闭</el-button>
      <el-button type="primary" @click="submitForm">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import FormBlockTitle from '@/components/ScFormBlockTitle/index'

export default {
  name: 'DrEditForm',
  components: { FormBlockTitle },
  props:{
    editFormParam:{
      type: Object,
      default: null
    }
  },
  data() {
    return {
      id: -1,
      // 弹出层标题
      title: '录入信息',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7
        }
      },
      daterangeOptions:{
        disabledDate (time) {
          // 禁用以后的时间
          return time.getTime() > Date.now() - 8.64e6
        },
      },
      dictOptionsMap:{},
      formType : 'add',
    }
  },
  created() {
    for(let i in this.editFormParam.editFormFieldList) {
      if(this.editFormParam.editFormFieldList[i].columnType === 'SELECT') {
        this.getDicts(this.editFormParam.editFormFieldList[i].dict).then(response => {
          this.dictOptionsMap[this.editFormParam.editFormFieldList[i].dict] = response.data
        })
      }
    }
  },
  methods: {
    init(row) {
      //编辑页面加载数据
      this.$emit('initEdit',row)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.$nextTick(() => {
        this.$refs['form'].resetFields()
      });
    },
    // 表单重置
    reset() {
      this.form = {}
    },
    /** 保存按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.formType === 'edit') {
            this.$emit('saveEdit',this.form)
          }else{
            this.$emit('saveAdd',this.form)
          }
        }
      })
    },
    getDict(dict) {
      return this.dictOptionsMap[dict]
    }
  }
}
</script>
